package class5;

import common.TreeNode;
import common.TreeNodeUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 二叉树的中序遍历
 * 给定一个二叉树的根节点 root ，返回 它的 中序 遍历 。
 */
public class Code01_inorderTraversal {

    public static void main(String[] args) {
        TreeNode root = getTreeNode();
        System.out.println(inorderTraversal(root));
    }

    private static TreeNode getTreeNode() {
        TreeNode treeNode1 = TreeNodeUtils.createTreeNode(1, 2, 3);
        TreeNode left = treeNode1.left;
        TreeNode right = treeNode1.right;
        TreeNode[] t = TreeNodeUtils.getTreeNodes(4, 5, 6, 7);
        left.left = t[0];
        left.right = t[1];
        right.left = t[2];
        right.right = t[3];
        return treeNode1;
    }

    public static List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        process(root, list);
        return list;
    }

    public static void process(TreeNode root, List<Integer> list) {
        if (root == null)
            return;
        process(root.left, list);
        list.add(root.val);
        process(root.right, list);
    }
}
